package com.cxrj.zhsq.personnelManage.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.cxrj.zhsq.personnelManage.dao.PropertyPersonnelMapper;
import com.cxrj.zhsq.personnelManage.entity.PropertyPersonnel;
import com.cxrj.zhsq.personnelManage.service.PropertyPersonnelService;

@Service
public class PropertyPersonnelServiceImpl implements PropertyPersonnelService {

	@Resource
	private PropertyPersonnelMapper propertyPersonnelMapper;

	@Override
	public Map<String, Object> seletePropertyPersonnelPage(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		sqlMap.put("start", Integer.parseInt(start));
		sqlMap.put("length", Integer.parseInt(length));
		sqlMap.put("communityId", request.getSession().getAttribute("communityId"));
		sqlMap.put("name", request.getParameter("name"));
		// 总记录数
		String recordsTotal = "0";
		// 获取数据库总记录数
		recordsTotal = propertyPersonnelMapper.selectByCount(sqlMap) + "";
		List<PropertyPersonnel> list = propertyPersonnelMapper.selectPropertyPersonnelsPage(sqlMap);

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);

		return resultMap;
	}

	@Override
	public Map<String, Object> insertPropertyPersonnel(PropertyPersonnel propertyPersonnel) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = 0;
		if (propertyPersonnel.getId().equals("")) {
			propertyPersonnel.setId(UUID.randomUUID().toString());
			propertyPersonnel.setState("1");
			propertyPersonnel.setCreateTime(new Date());
			c = propertyPersonnelMapper.insertSelective(propertyPersonnel);
		} else {
			c = propertyPersonnelMapper.updateByPrimaryKeySelective(propertyPersonnel);
		}
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> updatePropertyPersonnel(PropertyPersonnel propertyPersonnel) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = propertyPersonnelMapper.updateByPrimaryKeySelective(propertyPersonnel);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectPropertyPersonnelById(String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("personnel", propertyPersonnelMapper.selectByPrimaryKey(id));
		return resultMap;
	}

	@Override
	public Map<String, Object> selectPropertyPersonnelByAll(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("communityId", request.getSession().getAttribute("communityId"));
		List<PropertyPersonnel> list = propertyPersonnelMapper.selectPropertyPersonnelByAll(sqlMap);
		resultMap.put("list", list);
		return resultMap;
	}
}
